//index.js
//获取应用实例
var WxParse = require('../../wxParse/wxParse.js');
import { request } from '../../utils/request.js'
const app = getApp()
var timerInterval, timerTimeout;
Page({
  data: {
    isLoading:true,
    loginFail:false
  },
  //事件处理函数
  onLoad: function () {
    var that = this;
    console.log('singPage执行')
    if (!app.globalData.plumSession) {
      //回调
      app.plumSessionCallback = plumSession => {
        if (plumSession != '') {
          console.log('查询到登录信息');
          that.indexInfor();
        } else {
          console.log('未查询到登录信息');
          app.wechatSq(that);
        }
      }
    } else {
      that.indexInfor();
    }
  },
  onShow: function () {
    var that = this;
    var title = that.data.title;
    if (title) {
      wx.setNavigationBarTitle({
        title: title
      })
    }
  },
  indexInfor: function () {
    var that = this;
    request({
      data: {
        map: 'applet_start_page',
      },
    }).then(res => {
      app.globalData.followOpen = res.data.data.followOpen;
      if (res.data.ec == 200) {
        console.log(res.data.data)
        // that.requestIndex();
        var base = app.globalData.base;
        var versionBase = res.data.data.versionBase;
        if (res.data.data.isauth && base >= versionBase) {
          that.setData({
            detail: res.data.data,
            isauth: res.data.data.isauth,
            title: res.data.data.headTitle
          })
          var article = res.data.data.content;
          //article为连接数据接口时接收到的富文本数据信息       
          // 富文本解析
          WxParse.wxParse('article', 'html', article, that, 5)
          wx.setNavigationBarTitle({
            title: res.data.data.headTitle
          })
        } else {
          var jumpPagedata = {
            indexUrl: res.data.data.indexUrl,
            applyOpen: res.data.data.applyOpen
          }
          that.setData({
            jumpPagedata: jumpPagedata
          })
          var showStartImg = res.data.data.startImg;
          if (showStartImg) {
            that.setData({
              showStartImg: showStartImg,
              showStart: true
            })
            wx.setNavigationBarTitle({
              title: res.data.data.shopName
            })
            var remainTime = showStartImg.time;
            var leftCircle = "circleProgressLoad_left " + remainTime + "s linear forwards";
            var rightCircle = "circleProgressLoad_right " + remainTime + "s linear forwards";
            that.setData({
              remainTime: remainTime,
              leftCircle: leftCircle,
              rightCircle: rightCircle
            })
            timerInterval = setInterval(function () {
              if (remainTime > 0) {
                remainTime--;
              } else {
                remainTime = 0;
                clearInterval(timerInterval);
              }
              that.setData({
                remainTime: remainTime
              })
            }, 1000)

            timerTimeout = setTimeout(function () {
              that.jumpPage();
            }, (showStartImg.time) * 1000)
          } else {
            that.jumpPage();
          }
        }
      } else {
        console.log(res.data)
        app.errorTip(that, res.data.em, 2000);
      }
    }).catch(err => {
      console.log(err)
    })
  },
  hideStartimg:function(){
    var that = this;
    that.jumpPage();
  },
  jumpPage:function(){
    var that = this;
    clearInterval(timerInterval);
    clearTimeout(timerTimeout);
    console.log('清除定时器');
    var jumpPagedata = that.data.jumpPagedata;
    var indexUrl = jumpPagedata.indexUrl;
    if (jumpPagedata.applyOpen == 1) {
      wx.reLaunch({
        url: '/pages/login/login',
      })
    }else{
      var extindexUrl = app.globalData.indexUrl;
      indexUrl = indexUrl.substring(1);
      if (!that.isTabbarpath(indexUrl) && extindexUrl) {
        if (extindexUrl.indexOf('/pages')>=0){
          indexUrl = extindexUrl;
        }else{
          indexUrl = '/' + extindexUrl;
        }
        
      } else {
        if (indexUrl.indexOf('/pages') >= 0) {
          indexUrl = indexUrl;
        } else {
          indexUrl = '/' + indexUrl;
        }
      }
      console.log(indexUrl);
      wx.reLaunch({
        url: indexUrl
      })
    }
  },
  isTabbarpath: function (indexUrl){
    var that = this;
    var menu = app.globalData.menuTitle ? app.globalData.menuTitle:'';
    console.log(menu)
    var isTabpath = false;
    if (menu.hasOwnProperty(indexUrl)) {
      console.log(menu[indexUrl]);
      isTabpath = true;
    }
    return isTabpath;
  },
  openLocation: function (e) {
    var lng = Number(e.currentTarget.dataset.lng);
    console.log(typeof (lng));
    var lat = Number(e.currentTarget.dataset.lat);
    var name = this.data.detail.name;
    var address = this.data.detail.address;
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      name: name,
      address: address,
      scale: 18
    })
  },
  makeCall: function (e) {
    var phone = e.currentTarget.dataset.phone;
    wx.showModal({
      title: phone,
      content: '',
      confirmText: '拨打',
      confirmColor: '#48C23D',
      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: phone,
            success: function () {
              console.log("拨打电话成功！")
            },
            fail: function () {
              console.log("拨打电话失败！")

            }
          })
        }
      }
    })
  },
  onPullDownRefresh: function () {
    var that = this;
    that.onLoad();
    this.onReady();
    console.log("下拉刷新");
  },
  playVideo: function () {
    var that = this;
    that.setData({
      isShowVideo: true
    })
    this.videoContext.play();
  },
  hideVideo: function () {
    var that = this;
    that.setData({
      isShowVideo: false
    })
  },
  yyNameChange: function (e) {
    this.setData({
      yyName: e.detail.value
    })
  },
  yyTelChange: function (e) {
    this.setData({
      yyTel: e.detail.value
    })
  },
  yyMarkChange: function (e) {
    this.setData({
      yyMark: e.detail.value
    })
  },
  yuyueSubmit: function () {
    var that = this;
    var data = {};
    data.map = 'applet_submit_appointment';
    data.name = that.data.yyName;
    data.mobile = that.data.yyTel;
    data.content = that.data.yyMark;
    data.prompt = that.data.detail.appointButton;
    if (data.name.length <= 0) {
      app.errorTip(that, '您的姓名不能为空', 2000);
      return;
    }
    if (data.mobile.length != 11) {
      app.errorTip(that, '请输入正确的手机号', 2000);
      return;
    }
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          app.errorTip(that, res.data.data.msg, 2000);
          that.setData({
            yyName: "",
            yyTel: "",
            yyMark: ""
          })
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onShareAppMessage: function () {
    var that = this;
    var title = this.data.title;
    var shareInfo = app.globalData.shareInfo;
    title = shareInfo.shareTitle ? shareInfo.shareTitle : title;
    var cover = shareInfo.shareCover ? shareInfo.shareCover : '';
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      path: '/pages/singlePage/singlePage'
    }
  },
})
